package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hebing
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 根据手机号查询数据库
     *
     * @param phone
     * @return
     */
    @Override
    public User selectUser(String phone) {
        //设置查询条件
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        //调用mapper查询数据库
        User user = userMapper.selectOne(lqw);
        if(user!=null){
            return user;
        }else{
            User newUser = new User();
            newUser.setPhone(phone);
            newUser.setStatus(1);
            //为空 保存到数据库中
            int rows = userMapper.insert(newUser);
            return newUser;
        }
    }
}
